package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class SzjsCnNewsAnalyse implements AnalyseNews{
	
	private static final String[] regex={
				"http://www.sz.js.cn/news/[0-9a-z]+.shtml",
				"http://www.ly.js.cn/[a-z\\/]+[0-9\\/]*.shtml",
				"http://[a-z]+.wj.js.cn/[a-z\\/]+[0-9\\/]+.shtml",
				"http://[a-z]+.cs.js.cn/newsfile/[0-9\\-]+/[0-9]+.html",
				"http://news.ks.js.cn/item/show/[0-9]+.html",
				"http://[a-z]+.dfzs.js.cn/[a-z\\/]+[0-9\\/]+.shtml",
				"http://www.dafeng.js.cn/[a-z\\/]+[0-9]+.shtml",
	};
			
			public boolean isDetailPage(String url) {
				for (int i=0; i < regex.length; i++) {
					if (url.matches(regex[i])) {
						return true;
					}
				}
				return false;
			}
			
			
			public NewsMeta Update(NewsMeta meta) {
				return null;
			}

			

			
			public NewsMeta parserHtml(UrlMeta urlMeta) {
				NewsMeta news=new NewsMeta();
				if (urlMeta.getHtml() == null) {
				}		
				String htmltxt=urlMeta.getHtml();
				String url=urlMeta.getUrl();		
				
				String title=null;
				String content=null;
				Long date=null;
				String author=null;
				Document doc=Jsoup.parse(htmltxt);
				title=doc.select("span.news_title").text();
				if (StringUtils.isBlank(title)) {
					title=doc.select("h1").text();
				}
				if (StringUtils.isBlank(title)) {
					title =doc.select("div#list2left02").text();
				}
				if (StringUtils.isBlank(title)) {
					title =doc.select("span.xxbt2").text();
				}
				if (StringUtils.isBlank(title)) {
					title =doc.select("div.v_title").text();
				}
				if (StringUtils.isBlank(title)) {
					title =doc.select("div.content_hd h2").text();
				}
				
				date=DateUtils.matchDate(doc.select("span").text());
				if (date == null) {
					date=DateUtils.matchDate(doc.select("div.r").text());
				}
				if (date == null) {
					date=DateUtils.matchDate(doc.select("div.news_tit").text());
				}
				if (date == null) {
					date=DateUtils.matchDate(StringUtils.substringAfterLast(url, "/"));
				}
				if (date == null) {
					date=DateUtils.matchDate(doc.select("div.v_c_sm").text());
				}
				
				content=HtmlCleaner.getContentHtml(url,doc.select("div.article"));
				if (StringUtils.isBlank(content)) {
					content =HtmlCleaner.getContentHtml(url, doc.select("div#contentText"));
				}
				if (StringUtils.isBlank(content)) {
					content =HtmlCleaner.getContentHtml(url, doc.select("div.news_content"));
				}
				if (StringUtils.isBlank(content)) {
					content =HtmlCleaner.getContentHtml(url, doc.select("div#list2left04_20101125"));
				}
				if (StringUtils.isBlank(content)) {
					content =HtmlCleaner.getContentHtml(url, doc.select("td.tdtxt02"));
				}
				if (StringUtils.isBlank(content)) {
					content =HtmlCleaner.getContentHtml(url, doc.select("div.v_c_zw"));
				}
				if (StringUtils.isBlank(content)) {
					content =HtmlCleaner.getContentHtml(url, doc.select("div#endtext"));
				}
				if (StringUtils.isBlank(content)) {
					content =HtmlCleaner.getContentHtml(url, doc.select("div.Cnt-Article"));
				}
				
				author=JSoupUtils.matchAuthor(doc, "来源：");
				
				news.setAuthor(author);
				news.setUrl(url);		
				news.setTitle(title);
				news.setContent(content);
				news.setDate(date);
				return news;
			}
			
			public static void main(String[] args) {
				SzjsCnNewsAnalyse test=new SzjsCnNewsAnalyse();
			
				String url="http://www.dafeng.js.cn/dfxw/zxbd/156995.shtml";
				UrlMeta meta=CrawlHTML.responseToURL(url);
				url = UrlArgumentTop.FromatUrl(url);
				System.out.println(UrlArgumentTop.FromatUrl(url));
				System.out.println(test.isDetailPage(url));
				NewsMeta parserHtml=test.parserHtml(meta);
				System.out.println(parserHtml);
			}
	
	public boolean isNeedUpdate(){
		return false;
	}

}
